package com.oocl.o2o.client.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import com.oocl.o2o.client.entity.Customer;
import com.oocl.o2o.client.entity.ReceiverInfo;
import com.oocl.o2o.client.service.ReceiverInfoService;
import com.oocl.o2o.client.vo.ReceiverInfoVO;


@Controller
public class ReceiverInfoController {
	
	@Resource
	private ReceiverInfoService receiverInfoService;
	
	
	@ResponseBody
	@GetMapping("/receiverInfo/id/{id}")
	public ReceiverInfo findById(@RequestBody @PathVariable("id") String id){
		ReceiverInfo receiverInfo = new ReceiverInfo();
		receiverInfo = receiverInfoService.findById(id);
		return receiverInfo;
	}
	
	@ResponseBody
	@GetMapping("/receiverInfo/customerId/{customerId}")
	public List<ReceiverInfo> findByCustomerId(@PathVariable("customerId") String customerId){
		System.out.println("拿 "+ customerId + " 的ReceiverInfo");
		List<ReceiverInfo> receiverInfos = new ArrayList<ReceiverInfo>();
		receiverInfos = receiverInfoService.findByCustomerId(customerId);
		return receiverInfos;
	}
	
	@ResponseBody
	@PostMapping("/receiverInfo")
	public List<ReceiverInfo> add(@RequestBody ReceiverInfoVO receiverInfoVO,HttpSession session){
		ReceiverInfo r = new ReceiverInfo();
		Customer customer = (Customer) session.getAttribute("customer");
		if(customer == null){
			return null;
		}
		r = receiverInfoService.add(receiverInfoVO,customer);
		
		return receiverInfoService.findByCustomerId(customer.getId());
	}
	
	@ResponseBody
	@PatchMapping("/receiverInfo")
	public List<ReceiverInfo> update(@RequestBody ReceiverInfoVO receiverInfoVO, HttpSession session){
		Customer customer = (Customer) session.getAttribute("customer");
		ReceiverInfo receiverInfo = receiverInfoService.findById(receiverInfoVO.getId());
		receiverInfo.setName(receiverInfoVO.getName());
		receiverInfo.setAddress(receiverInfoVO.getAddress());
		receiverInfo.setPhone(receiverInfoVO.getPhone());
		ReceiverInfo  r = new ReceiverInfo();
		r = receiverInfoService.update(receiverInfo);
		return receiverInfoService.findByCustomerId(customer.getId());
	}
	@ResponseBody
	@DeleteMapping("/receiverInfo/customerId/{customerId}/receiverInfoId/{receiverInfoId}")
	public List<ReceiverInfo> delete(@PathVariable("customerId") String customerId, @PathVariable("receiverInfoId") String receiverInfoId){
		ReceiverInfo receiverInfo = new ReceiverInfo();
		receiverInfo = receiverInfoService.delete(receiverInfoId);
		return receiverInfoService.findByCustomerId(customerId);
		
	}
	
	@GetMapping("/receiverInfo")
	public String actionToJsp(){
		return "view/receiverInfo";
	}
	
}
